#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4

from django.db import models
import datetime

def utcnow():
    return datetime.datetime.now()

'''
    公共数据库表,其他模块的表放到模块所在的包
    规范：首字母大写，定义db_table
'''
class Base(models.Model):
    created_at = models.DateTimeField(null=True, blank=True)
    updated_at = models.DateTimeField(null=True, blank=True)
    deleted_at = models.DateTimeField(null=True, blank=True)
    deleted = models.BooleanField(default=False)
    class Meta:
        abstract = True
    
    def delete(self):
        self.deleted = 1
        self.deleted_at = utcnow()
        self.save()
        
    def update(self,values):
        for k,v in values.items():
            setattr(self, k, v)
        
    def save(self, force_insert=False, force_update=False, using=None):
        self.updated_at = utcnow()
        if not self.created_at:
            self.created_at = utcnow()
        super(Base, self).save(force_insert=force_insert, force_update=force_update, using=using)
        

class Account(Base):
    """额度账户表"""
    account = models.CharField(max_length=225,unique=True)                #店主账户：10，代理：11
    loginname = models.CharField(max_length=64, null=True,blank=True,unique=True)  #登录名
    name = models.CharField(max_length=64,null=True,blank=True)  #真实姓名
    identify = models.CharField(max_length=64,null=True, blank=True) #身份证号码
    address = models.CharField(max_length=64,null=True,blank=True) #彩票机地址
    telephone = models.CharField(max_length=15,null=True,blank=True) #手机号码
    bankname = models.CharField(max_length=64,null=True,blank=True) #具体开户行名称
    password = models.CharField(max_length=64)                #密码，MD5+base64加密
    balance = models.FloatField()                             #账户余额
    bankNum = models.CharField(max_length=64,null=True, blank=True)       #银行帐号  
    commissionRatio = models.FloatField(null=True, blank=True)            #提成点
    accountType = models.IntegerField()              #账户类型：店主账户：0，代理商账户：1，公司账户：2
    parentid = models.IntegerField(null=True, blank=True)                  #归属账户id
    mac = models.CharField(max_length=64,null=True, blank=True)            #绑定终端
    logintime=models.DateTimeField(null=True,blank=True)
    loginip=models.CharField(max_length=20,null=True,blank=True)
    desc = models.CharField(max_length=225,null=True, blank=True)          #描述信息
    
    class Meta:
        db_table = u'account'   

class Period(Base):
    '''期次信息表'''
    lotterycode = models.CharField(max_length=20)
    periodid = models.CharField(max_length=64)  #期次id
    starttime = models.DateTimeField()
    endtime = models.DateTimeField()
    awardtime = models.DateTimeField()
    poolmoney = models.IntegerField(blank=True,null=True)
    awardcodes = models.CharField(max_length=64,blank=True,null=True)
    status = models.IntegerField()  # 0:未查询中奖名单列表，1：已经查询中奖名单
    
    class Meta:
        db_table = u'period'
  

class Trade(Base):
    """交易表"""
    batchid = models.CharField(max_length=20,null=True,blank=True,unique=True)
    tradeType = models.IntegerField()                #交易类型:  充值：0，购买彩票：1， 提现：2，返奖：3
    tradeTicketType = models.IntegerField(null=True, blank=True)  #只针对购买彩票，标明购买的彩票类型： 时时彩：0， 双色球：1, 七乐彩：2，福彩3D：3，15选5：4，东方6+1：5
    tradeMethod = models.IntegerField(null=True, blank=True)     #前置条件：购买彩票，交易方式：刷卡：0，投币：1，店主代购：2
    prizeType = models.IntegerField(null=True, blank=True)  #返奖方式，0：现金返奖， 1：银联返奖
    tradeAmount = models.FloatField()                #交易金额
    accountid = models.ForeignKey(Account)                #交易账户
    prizeaccount = models.ForeignKey(Account,null=True,blank=True,db_column='prize_account_id',related_name='prize_account')  #返奖账户
    approver = models.ForeignKey(Account,null=True,blank=True,db_column='approver_id',related_name='approver')  #审批操作员
    cashier = models.ForeignKey(Account,null=True,blank=True,db_column='cashier_id',related_name='cashier')  #现金发放操作员
    tradeTime = models.DateTimeField()               #交易时间
    status = models.IntegerField()  # 交易中：0，交易失败：1，交易成功：2，交易取消：3
    charges = models.FloatField(default=0.0,null=True, blank=True)  #每笔交易产生的手续费
    period = models.ForeignKey(Period,null=True,blank=True) #交易的期次信息
    desc = models.CharField(max_length=64,null=True, blank=True) #购买彩票收单批次号
    
    class Meta:
        db_table = u'trade'
        

class BaseTicket(Base):
    '''彩票基本表，所有彩票继承该类'''
    lotteryNumber = models.CharField(max_length=128)   #彩票号码
    ticketamount = models.IntegerField() #彩票注数
    buytime = models.DateTimeField()                  #购买时间    
    validitytime = models.DateTimeField()             #有效时间
    prizetime = models.DateTimeField(null=True, blank=True)                #兑奖时间
    status = models.IntegerField()                    #交易中：0，交易失败：1，交易成功：2，交易取消：3, 已兑奖:4
    buymoney = models.IntegerField()                  #付款金额
    prizemoney = models.IntegerField(null=True, blank=True)                #中奖金额
    desc = models.CharField(max_length=64,null=True, blank=True)  #描述信息
    tradeId = models.ForeignKey(Trade,db_column='tradeId')                  #交易表外键
    businessid = models.CharField(max_length=64) #流水号
    hasbigprize = models.CharField(max_length=4,null=True,blank=True)  #是否有大奖
    
    class Meta:
        abstract = True
    
class TicketConstant(BaseTicket):
    """时时彩彩票表，表结构进行拆分，防止大表出现"""
    class Meta:
        db_table = u'ticket_constant'


class TicketLoan(BaseTicket):
    """双色球彩票表，表结构进行拆分，防止大表出现"""
    
    class Meta:
        db_table = u'ticket_loan'
        
class SevenColor(BaseTicket):
    """七乐彩彩票表，表结构进行拆分，防止大表出现"""
    class Meta:
        db_table = u'seven_color'


class ThreeD(BaseTicket):
    """福彩3D彩票表，表结构进行拆分，防止大表出现"""
    class Meta:
        db_table = u'three_D'


class FiveBall(BaseTicket):
    """15选5彩票表，表结构进行拆分，防止大表出现"""
    class Meta:
        db_table = u'five_ball'


class SixPlusOne(BaseTicket):
    """东方6+1彩票表，表结构进行拆分，防止大表出现"""
    class Meta:
        db_table = u'six_plus_one'


class Lotterycitizens(Base):
    """彩票彩民表"""
    trade = models.ForeignKey(Trade)                #彩票表外键
    bankAccount = models.CharField(max_length=64)    #彩民银行卡帐号

    class Meta:
        db_table = u'lotterycitizens'


class Commission(Base):
    """提成统计表"""
    account = models.ForeignKey(Account)                     #account表外键
    commissionRatio = models.FloatField()                 #提成点
    salesAmount = models.FloatField()                     #销售额
    commission = models.FloatField()                      #提成金额
    desc = models.CharField(max_length=225,null=True, blank=True)     #描述

    class Meta:
        db_table = u'commission'
        

class Statistics(Base):
    """数据统计表"""
    account = models.ForeignKey(Account)                     #account表外键
    bankRatio = models.FloatField()
    commissionRatio = models.FloatField()                 #提成点
    salesAmount = models.FloatField()                     #销售额
    salesCard = models.FloatField()                       #刷卡销售额统计
    salesCash = models.FloatField()                       #投币销售额统计
    shopkeeper = models.FloatField()                       #店主代购销售额统计
    commission = models.FloatField()                      #提成金额
    bankcomission = models.FloatField()                   #银联手续费
    statistics_month = models.CharField(max_length=20)
    status = models.IntegerField() #统计数据状态,0:待审核， 1：审核通过，2：提成发放完毕
    desc = models.CharField(max_length=64,null=True, blank=True)     #描述

    class Meta:
        db_table = u'statistics'
  
  
class SystemSettings(Base):
    '''系统设置表，用于各种通用设置。如手续费扣款日,额度账户告警阀值等'''
    itemname = models.CharField(max_length=64) #设置项
    itemvalue = models.CharField(max_length=64) #设置项的值
    itemvalue2 = models.CharField(max_length=64,null=True,blank=True) #新值
    legaldate = models.DateTimeField(null=True,blank=True)  #新的设置生效日
    desc = models.CharField(max_length=256,blank=True,null=True) #设置描述信息
    
    class Meta:
        db_table = u'system_settings'
        
class Logs(Base):
    '''日志表'''
    logtype = models.CharField(max_length=20) #管理员日志，系统日志
    logcontent = models.CharField(max_length=64) #日志内容
    logaccount = models.ForeignKey(Account)
    
    class Meta:
        db_table = u'lottery_logs'
    
    
class SerialTerminalNumber(Base):
    '''终端号序列号表，易宝的所有终端号与序列号，新的终端从这张表获取未使用的终端号和序列号'''
    terminal_number = models.CharField(max_length=20,unique=True)
    serial_number = models.CharField(max_length=20,unique=True)
    is_used = models.BooleanField(default=False)
    class Meta:
        db_table = u'terminal_serial_number'
        